//
//  AttachList.m
//  xjairOA
//
//  Created by apple on 15-12-26.
//  Copyright (c) 2015年 net. All rights reserved.
//

#import "AttachList.h"
#import "ZipArchive.h"
#import "AttachementContent.h"
@implementation AttachList
NSArray * array;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"附件列表";
    self.tableview.dataSource= self;
    self.tableview.delegate = self;
    //NSLog(@"finalDocumentName:%@",self.finalDocumentName);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *docPath = [documentsDirectory stringByAppendingString:@"/中文名.zip"];
    //NSLog(@"#######%@",docPath);
    NSString * urlAsString = @"http://172.16.3.126:8080/iosServer/attaches/test.zip";
    NSURL * url = [NSURL URLWithString:urlAsString];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    NSError * error = nil;
    NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    if(data !=nil){
        //NSLog(@"下载成功");
        if([data writeToFile:docPath atomically:YES]){
           // NSLog(@"保存成功");
            
        }
        else{
            //NSLog(@"保存失败");
        }
    }else{
        NSLog(@"%@",error);
    }
    
        ZipArchive* zip = [[ZipArchive alloc] init];
        NSString * unzipto = [documentsDirectory stringByAppendingString:@"/test"];
        if ([zip UnzipOpenFile:docPath]) {
            BOOL ret = [zip UnzipFileTo:unzipto overWrite:YES];
            if(ret==NO){};
            [zip UnzipCloseFile];
        }
    
        NSFileManager * fm = [NSFileManager defaultManager];
        array = [fm contentsOfDirectoryAtPath:unzipto error:nil];
//        for(NSString * item in array){
//            NSLog(@"%@",item);
//        }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return array.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"attachementCell" forIndexPath:indexPath];
    UILabel * label = (UILabel*)[cell viewWithTag:1];
    //label.text = @"娃娃.doc";
    NSUInteger rowNo = indexPath.row;
    label.text = [array objectAtIndex:rowNo];
    return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UITableViewCell * cell = (UITableViewCell*)sender;
    NSIndexPath * indexPath = [self.tableview indexPathForCell:cell];
    AttachementContent * attachmentContent = (AttachementContent*)segue.destinationViewController;
    [attachmentContent setValue:[array objectAtIndex:indexPath.row] forKey:@"DocumentName"];
}
@end
